package com.thinkwide.data.ormapping.sql;

import com.thinkwide.data.core.MFString;
import com.thinkwide.data.exchange.MFExchange;

import java.util.List;

public class MFSqlDelete extends MFSqlBase {
    public static final String DELETE = "Delete From %s %s ";
    public static final String DELETE_SIMPLE = "Delete  From %s ";

    public static String getDeleteSql(String tableName, String where, Object... args) {
        String strWhere = WHERE + getStaticSql(where, args);
        String strSQL = String.format(DELETE, tableName, strWhere);
        return strSQL;

    }

    public static String getDeleteSql(String tableName, String where) {
        String strSQL = String.format(DELETE, tableName, WHERE + where);
        return strSQL;
    }

    public static String getDeleteListSql(String tableName, String where, String primaryKeys, String values) {

        List<String> klist = MFExchange.getTextToList(primaryKeys, ",");
        List<String> vlist = MFExchange.getTextToList(values, ",");
        int i = 0;
        for (String item : klist) {
            String value = vlist.get(i);
            if (MFString.notNullOrEmpty(where)) {
                where = where + AND;
            }
            where = where + item + "=" + MFString.quoteStrAll(value);
            i++;
        }

        String sSQL = MFSqlDelete.getDeleteSql(tableName, where);
        return sSQL;
    }

    public static String getDeleteListSql(String tableName, String primaryKeys, String values) {

        return getDeleteListSql(tableName, "", primaryKeys, values);
    }

    public static String getDeleteListSql(String tableName, String where, List<String> primaryKeys, List<String> values) {


        int i = 0;
        for (String item : primaryKeys) {
            String value = values.get(i);
            if (MFString.notNullOrEmpty(where)) {
                where = where + AND;
            }
            where = where + item + "=" + MFString.quoteStrAll(value);
            i++;
        }

        String sSQL = MFSqlDelete.getDeleteSql(tableName, where);
        return sSQL;
    }

    public static String getDeleteListSql(String tableName, List<String> primaryKeys, List<String> values) {

        String where = MFString.Empty;

        for (String item : primaryKeys) {
            if (MFString.notNullOrEmpty(where)) {
                where = where + AND;
            }
            where = where + item + "= ? ";
        }

        String sSQL = MFSqlDelete.getDeleteSql(tableName, where);
        return sSQL;
    }

    public static String getDeleteSqlParam(String tableName, List<String> primaryKeys) {

        String where = MFString.Empty;

        for (String item : primaryKeys) {
            if (MFString.notNullOrEmpty(where)) {
                where = where + AND;
            }
            where = where + item + "= ? ";
        }

        String sSQL = MFSqlDelete.getDeleteSql(tableName, where);
        return sSQL;
    }
}
